<?php

namespace app\admin\controller;

use app\admin\controller\adminBase;
use think\facade\View;
use app\common\model\mysql\AdminUser;
class Login extends adminBase{

    public function initialize()
    {
        if($this->isLogin()){
            return $this->redirect(url('index/index'));
        }
    }
    public function md5(){
        halt(session(config("admin.session_admin")));
    }

    public function index(){
        return View::fetch();
    }
    public function check(){

            if(!$this->request->isPost()){
                return show(config('status.error'),'请求方式错误');
            }
            $username = $this->request->param("username", '', "trim");
            $password = $this->request->param("password", '', "trim");
            $captcha = $this->request->param("captcha", '', "trim");

            $data = [
                'username' => $username,
                'password' => $password,
                'captcha' => $captcha,
            ];

            $validate = new \app\admin\validate\AdminUser();
            if(!$validate->check($data)){
                return show(config('status.error'),$validate->getError());
            }
            try {
                $adminUserObj = new \app\admin\business\AdminUser();
                $adminUserObj->login($data);
                return show(config('status.success'),'登录成功');

            }catch (\Exception $exception){
                return show(config('status.error'),$exception->getMessage());
            }

    }
}